//
//  AllCommentsViewController.m
//  SeaMelonSeeds_Counselor
//
//  Created by 尤振宇 on 2017/7/21.
//  Copyright © 2017年 杨笑. All rights reserved.
//

#import "AllCommentsViewController.h"
#import "CommentsTableViewCell.h"
#import "QuestionAnswer.h"
#import "QuestionAnswerView.h"
#import "QuestionAnswerApi.h"
#import "PersonalDetailsViewController.h"

@interface AllCommentsViewController () <UITextFieldDelegate>
@property(nonatomic,strong)QuestionAnswerView *questionAnswerView;
@property(nonatomic,strong)UIView *header;
@property(nonatomic,weak)UILabel *allAnswer;
@property(nonatomic,strong)UITextField *answerTF;
@end

@implementation AllCommentsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.fromTop.constant = 64;
    self.headerViewH.constant = 0;
    self.fromBottom.constant = 50;
    self.tableView.backgroundColor = [UIColor whiteColor];
    
    [self loadData];
    [self addMj_header];
    [self addMj_footer];
    
    [self.tableView registerCellWithNib:@"CommentsTableViewCell"];

    self.answerTF.placeholder = @"我要回答";
    self.title = @"全部回复";
    // Do any additional setup after loading the view.
}

-(void)loadData
{
    if (!self.listApi) {
        self.listApi = [[QuestionAnswerApi alloc]init_TalkReplys:self.questionAnswer.id];
    }
    WeakSelf(self)
    [self.listApi startWithCompletionBlockWithSuccess:^(__kindof YTKBaseRequest * _Nonnull request) {
        if (kCode(request.responseJSONObject)) {
            if (weakself.listApi.page == 1) {
                [weakself.dataArray removeAllObjects];
            }
            NSDictionary *data = [request.responseJSONObject objectForKey:@"data"];
            for (NSDictionary *dic in [data objectForKey:@"list"]) {
                Comments *obj = [[Comments alloc]init];
                [obj setValuesForKeysWithDictionary:dic];
                [weakself.dataArray addObject:obj];
            }
            weakself.allAnswer.text = [NSString stringWithFormat:@"全部回答 (%d)",[[data objectForKey:@"total"] intValue]];
        }
        [weakself.tableView reloadData];
        [weakself endRefreshing];
    } failure:^(__kindof YTKBaseRequest * _Nonnull request) {
        [weakself endRefreshing];
    }];
}

-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    if (self.questionAnswer.isPopUpKeyboard) {
        self.questionAnswer.isPopUpKeyboard = NO;
        [self.answerTF becomeFirstResponder];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CommentsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CommentsTableViewCell" forIndexPath:indexPath];
    
    Comments *obj = self.dataArray[indexPath.row];
    [cell.image sd_setImageWithURL:[NSURL URLWithString:obj.counselor.avatar] placeholderImage:[UIImage imageNamed:@"AnonymousAvatar"]];
    cell.comments.text = obj.body;
    cell.job.text = [NSString stringWithFormat:@"  %@  ",@"咨询师"];
    cell.name.text = obj.counselor.name;
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    Comments *obj = self.dataArray[indexPath.row];
    return [obj text_H] + 55;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return self.header;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return [self.questionAnswer text_H] + 75.5/*内容label的y*/ + 57;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    
    WeakSelf(self)
    QuestionAnswerApi *api = [[QuestionAnswerApi alloc]init_TalkReply:textField.text talkID:self.questionAnswer.id];
    [api startWithCompletionBlockWithSuccess:^(__kindof YTKBaseRequest * _Nonnull request) {
        if (kCode(request.responseJSONObject)) {
            textField.text = @"";
            
            weakself.listApi.page = 1;
            [self loadData];
        }
    } failure:^(__kindof YTKBaseRequest * _Nonnull request) {
        
    }];
    return YES;
}

-(UIView *)header
{
    if (!_header) {
        _header = [[UIView alloc]init];

        [_header addSubview:self.questionAnswerView];
        [self.questionAnswerView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_header);
            make.leading.equalTo(_header);
            make.trailing.equalTo(_header);
        }];
        
        UIView *back = [UIView new];
        back.backgroundColor = [UIColor whiteColor];
        [_header addSubview:back];
        [back mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_header);
            make.top.equalTo(self.questionAnswerView.mas_bottom).with.offset(5);
            make.width.equalTo(_header);
            make.height.equalTo(@40);
        }];
        
        UILabel *label = [[UILabel alloc]init];
        self.allAnswer = label;
        label.font = [UIFont systemFontOfSize:16];
        label.text = @"全部回答 (0)";
        [back addSubview:label];
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(_header);
            make.bottom.equalTo(back).with.offset(-5);
        }];
        
        UIView *view = [[UIView alloc]init];
        view.backgroundColor = kF2F2F2;
        [back addSubview:view];
        [view mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.questionAnswerView.mas_bottom);
            make.height.equalTo(@5);
            make.left.equalTo(back);
            make.right.equalTo(back);
        }];
        
        UIView *line = [[UIView alloc]init];
        line.backgroundColor = kF2F2F2;
        [back addSubview:line];
        [line mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(back);
            make.height.equalTo(@1);
            make.left.equalTo(back);
            make.right.equalTo(back);
        }];
    }
    return _header;
}

-(QuestionAnswerView *)questionAnswerView
{
    if (!_questionAnswerView) {
        _questionAnswerView = [[QuestionAnswerView alloc]init];
        [_questionAnswerView.image sd_setImageWithURL:[NSURL URLWithString:self.questionAnswer.user_avatar] placeholderImage:[UIImage imageNamed:@"AnonymousAvatar"]];
        _questionAnswerView.questionTitle.text = self.questionAnswer.title;
        _questionAnswerView.questionContent.attributedText = self.questionAnswer.content;
        _questionAnswerView.time.text = self.questionAnswer.created_at;
        _questionAnswerView.name.text = self.questionAnswer.user_nickname;
        
        WeakSelf(self)
        _questionAnswerView.clickImageAction = ^(id obj) {
            [weakself pushViewController:[PersonalDetailsViewController PersonalDetailsViewController:nil UserID:self.questionAnswer.user_id] animated:YES];
        };
    }
    return _questionAnswerView;
}

-(UITextField *)answerTF
{
    if (!_answerTF) {
        _answerTF = [[UITextField alloc]init];
        _answerTF.backgroundColor = [UIColor whiteColor];
        _answerTF.returnKeyType = UIReturnKeySend;
        _answerTF.delegate = self;
        _answerTF.enablesReturnKeyAutomatically = YES;
        
        [_answerTF roundedCorners:15];
        
        UIView *view = [[UIView alloc]init];
        view.backgroundColor = [@"f5f5f5" colorWithHexString];
        
        [self.view addSubview:view];
        WeakSelf(self)
        [view mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(weakself.view);
            make.left.equalTo(weakself.view);
            make.width.equalTo(weakself.view);
            make.height.equalTo(@(weakself.fromBottom.constant));
        }];
        
        UIView *leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 20, 30)];
        leftView.backgroundColor = _answerTF.backgroundColor;
        
        _answerTF.leftView = leftView;
        _answerTF.leftViewMode = UITextFieldViewModeAlways;
        
        [view addSubview:_answerTF];
        [_answerTF mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(weakself.view).with.offset(30);
            make.right.equalTo(weakself.view).with.offset(-30);
            make.center.equalTo(view);
            make.height.equalTo(@(weakself.fromBottom.constant - 20));
        }];
    }
    return _answerTF;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
